package com.db.williamchart;

import ohos.agp.render.BlendMode;
import ohos.agp.render.Paint;
import ohos.agp.render.Shader;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;

public class Painter {
    private Paint paint = new Paint();
    private Font labelFont;

    public Painter(){
        paint.setTextAlign(TextAlignment.CENTER);
        labelFont = Font.DEFAULT;
    }


    public float measureLabelWidth(String text,float textSize){
        paint.setTextSize((int) textSize);
        if (labelFont != null){
            paint.setFont(labelFont);
        }
        return paint.measureText(text);
    }

    public float measureLabelHeight(float textSize){
        paint.setTextSize((int) textSize);
        return paint.descent() - paint.ascent();
    }
    public float measureLabelAscent(float textSize){
        paint.setTextSize((int) textSize);
        return paint.ascent();
    }

    public float measureLabelDescent(float textSize) {
        paint.setTextSize((int) textSize);
        return paint.descent();
    }

    public void setLabelFont(Font labelFont) {
        this.labelFont = labelFont;
    }
    public void prepare(
            Float textSize,
            Integer color,
            Paint.Style style,
            Float strokeWidth,
            Shader shader,
            Paint.ShaderType shaderType,
            Font font
    ){
        if (textSize != null){
            paint.setTextSize((int) textSize.floatValue());
        }else {
            paint.setTextSize(15);
        }
        if (color != null){
            paint.setColor(new Color(color));
        }else {
            paint.setColor(new Color(-0x1000000));
        }
        if (style != null){
            paint.setStyle(style);
        }else {
            paint.setStyle(Paint.Style.FILL_STYLE);
        }
        if (strokeWidth != null){
            paint.setStrokeWidth(strokeWidth);
        }else {
            paint.setStrokeWidth(4f);
        }
        if (shader != null && shaderType != null){
            paint.setShader(shader,shaderType);
        }
        if (font != null){
            paint.setFont(font);
        }
    }
    public void setBlendMode(BlendMode blendMode){
        paint.setBlendMode(blendMode);
    }
    public void reset(){
        paint.reset();
    }

    public Paint getPaint() {
        return paint;
    }
}
